from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from nlc.models import User, Group
from nlc.serializers import user_serializer, group_serializer
from django.contrib.auth.hashers import check_password


class JSONResponse(HttpResponse):
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)


def user_list(request):
    if request.method == 'GET':
        data = User.objects.all()
        serializer = user_serializer(data, many=True)
        return JSONResponse(serializer.data)


def group_list(request):
    if request.method == 'GET':
        data = Group.objects.all()
        serializer = group_serializer(data, many=True)
        return JSONResponse(serializer.data)


@csrf_exempt
def users_detail(request, user_login_name):
    try:
        users = User.objects.get(user_login_name=user_login_name)
    except User.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = user_serializer(users)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = user_serializer(users, data=data)
        if serializer.is_valid() and check_password(serializer.user_pass, User.user_pass):
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        users.delete()
        return HttpResponse(status=204)


@csrf_exempt
def group_detail(request, group_login_name):
    try:
        groups = Group.objects.get(group_login_name=group_login_name)
    except User.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = group_serializer(groups)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = group_serializer(groups, data=data)
        if serializer.is_valid() and check_password(serializer.group_pass, Group.group_pass):
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        groups.delete()
        return HttpResponse(status=204)


@csrf_exempt
def data_register(request):
    if request.method == 'POST':
        data = JSONParser().parse(request)
        print(data)
        if data['flag'] == 'user':
            del data['flag']
            serializer = user_serializer(data=data)
            if serializer.is_valid():
                serializer.save()
                return JSONResponse({"result": "success", "description": ""}, status=201)
            if len(serializer.errors.values()) > 0:
                error_message = ";".join(serializer.errors.values()[0])
            else:
                error_message = ""
            return JSONResponse({"result": "fail", "description": error_message}, status=400)
        if data['flag'] == 'group':
            del data['flag']
            serializer = group_serializer(data=data)
            if serializer.is_valid():
                serializer.save()
                return JSONResponse({"result": "success", "description": ""}, status=201)
            if len(serializer.errors.values()) > 0:
                error_message = ";".join(serializer.errors.values()[0])
            else:
                error_message = ""
            return JSONResponse({"result": "fail", "description": error_message}, status=400)
        return HttpResponse()
'''
@csrf_exempt
def user_register(request):
    if request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = user_serializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)
'''